Ruby Under a Microscope: An Illustrated Guide to Ruby Internals by Pat Shaughnessy

Ruby Under a Microscope: An Illustrated Guide to Ruby Internals by Pat Shaughnessy

Author:Pat Shaughnessy
Language: eng
Format: mobi, pdf
Tags: COMPUTERS / Programming Languages / Ruby
ISBN: 9781593275617
Publisher: No Starch Press
Published: 2013-11-06T16:00:00+00:00


Chapter 8. How Ruby Borrowed a Decades-Old Idea from Lisp

Blocks are Ruby’s implementation of closures.

Blocks are one of the most commonly used and powerful features of Ruby because they allow you to pass a code snippet to Enumerable methods, such as each, detect, or inject. Using the yield keyword, you can also write your own custom iterators or functions that call blocks for other reasons. Ruby code containing blocks is often more succinct, elegant, and expressive than equivalent code in older languages, such as C.

But don’t jump to the conclusion that blocks are a new idea! In fact, blocks are not new to Ruby at all. The computer science concept behind blocks, called closures, was first invented by Peter J. Landin in 1964, a few years after the original version of Lisp was created by John McCarthy in 1958. Closures were later adopted by Lisp, or — more precisely — a dialect of Lisp called Scheme, which was invented by Gerald Sussman and Guy Steele in 1975. Sussman and Steele’s use of closures in Scheme brought the idea to many programmers for the first time.

But what does the word closure actually mean in this context? In other words, exactly what are Ruby blocks? Are they just the snippet of Ruby code that appears between the do and end keywords? In this chapter I’ll review how Ruby implements blocks internally and demonstrate how they meet the definition of closure used by Sussman and Steele back in 1975. I’ll also show how blocks, lambdas, and procs are all different ways of looking at closures.



Download



Copyright Disclaimer:
This site does not store any files on its server. We only index and link to content provided by other sites. Please contact the content providers to delete copyright contents if any and email us, we'll remove relevant links or contents immediately.